/**
  ******************************************************************************
  * @file    main.c
  * @author  MMY division
  * @version V1.3.0
  * @date    09/2012
  * @brief   Main program body
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "M24LR04E-R.h"
#include "delay.h"
#include "hw.h"
#include "itmc328.h"

static ErrorStatus AES_ReadPrivateKey(uint8_t *pPrivateKey);
static ErrorStatus AES_CheckPrivateKey(void);
static ErrorStatus AES_GetPrivateKey(uint8_t PayloadLength, uint8_t *privKeyBuffer);
static int8_t WriteFirmwareVersion(void);
static ErrorStatus saveJpg(uint8_t b);

volatile char FLASH_CR2     @0x5051;    
volatile char FLASH_NCR2    @0x17ff;    
volatile char FLASH_IAPSR   @0x5054;    
EEPROM uint8_t EEInitial = 0;
NEAR uint8_t testFileJpg_buffer[1100] = {0};
uint16_t testFileJpg_i = 0;

//const uint8_t FirmwareVersion = 0x01; // 4 MSB bits major version number & 4 LSB bits minor version number

void main(void) {
	uint8_t PayloadLength, bufMessage[50];
	bool err = TRUE;
	volatile uint8_t startDbg = 1;
	uint16_t cntErr = 0;

	HW_InitBoard();

	if(EEInitial == 0){
		WriteFirmwareVersion();
		EEInitial = 1;
	}

	while(1){
		err = TRUE;
		if(startDbg && AES_ReadPrivateKey(bufMessage) == SUCCESS){
			Itmc328_Init();
			if(ITMC328_OK == Itmc328_TakeAPicture(&saveJpg)){
				uint16_t size;
				testFileJpg_i = 0;
				if(ITMC328_OK == Itmc328_GetImageData(&size))
					err = FALSE;
			}
			Itmc328_DeInit();
			delay_ms(300);
			if(err)
				cntErr++;
		}
	}
}	


static ErrorStatus AES_ReadPrivateKey(uint8_t *pPrivateKey){
	ErrorStatus result = ERROR;
	uint8_t privKeyLength;
	uint8_t NbAttempt = 2;
		
	while(NbAttempt-- && result == ERROR){
		M24LR04E_Init();

		if((result = AES_CheckPrivateKey()) == SUCCESS){
			AES_GetPrivateKey(privKeyLength, pPrivateKey);
		}
	}

	M24LR04E_DeInit();
	return result;
}

/**
  * @brief this function checks if a NDEF message is available in the M24LR04E-R EEPROM
	* @par PayloadLength : the number of byte of the NDEF message
  * @retval SUCCESS : A NDEF message has been found
	* @retval ERROR :  a NDEF message doens't have been found
  */
static int8_t WriteFirmwareVersion(void){
	uint16_t WriteAddr = 0x01FC;
	uint8_t fwVer = 0x12;

	M24LR04E_Init();
	M24LR04E_WriteOneByte(M24LR16_EEPROM_ADDRESS_USER, WriteAddr, fwVer);
	M24LR04E_DeInit();

	return SUCCESS;
}


//TODO: modificare secondo le esigenze
static ErrorStatus AES_CheckPrivateKey(void){
	uint8_t OneByte = 0x00;
	uint16_t ReadAddr = 0x0000;

	M24LR04E_ReadOneByte (M24LR16_EEPROM_ADDRESS_USER, ReadAddr, &OneByte);	
	ReadAddr = 0x0009;
	M24LR04E_ReadOneByte (M24LR16_EEPROM_ADDRESS_USER, ReadAddr, &OneByte);	

	return SUCCESS;	
}

static ErrorStatus AES_GetPrivateKey(uint8_t PayloadLength, uint8_t *privKeyBuffer){
	M24LR04E_ReadBuffer(M24LR16_EEPROM_ADDRESS_USER, M24LR16_PRIVATE_KEY_ADDRESS, PayloadLength, privKeyBuffer);

	return SUCCESS;	
}



static ErrorStatus saveJpg(uint8_t b){
	testFileJpg_buffer[testFileJpg_i++] = b;

	if(testFileJpg_i == countof(testFileJpg_buffer)){
		//...
		testFileJpg_i = 0;
	}

	return SUCCESS;
}
